#define _CRT_SECURE_NO_WARNINGS	

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
int Count_Node(ListNode* head)
{
    int count = 0;
    ListNode* pcur = head;
    while (pcur)
    {
        count++;
        pcur = pcur->next;
    }
    return count;
}
ListNode* Buy_Node(int x)
{
    ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
    newnode->next = NULL;
    newnode->val = x;
    return newnode;
}
void Add_Node(ListNode* head, int x)
{
    ListNode* pcur = head;
    while (pcur->next)
    {
        pcur = pcur->next;
    }
    while (x)
    {
        pcur->next = Buy_Node(0);
        pcur = pcur->next;
        x--;
    }
}

void Sum_List(ListNode* longlist, ListNode* shortlist, int max)
{
    if (max == 0)
        return;
    else
    {
        if (shortlist->val + longlist->val < 10)
            shortlist->val = shortlist->val + longlist->val;
        else
        {
            shortlist->val = shortlist->val + longlist->val - 10;
            if (shortlist->next == NULL)
            {
                shortlist->next = Buy_Node(0);
            }
            shortlist->next->val++;
        }
        shortlist = shortlist->next;
        longlist = longlist->next;
        max--;
        Sum_List(longlist, shortlist, max);
    }
}

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
    ListNode* longlist = l1;
    ListNode* shortlist = l2;
    int len1 = Count_Node(longlist);
    int len2 = Count_Node(shortlist);
    int gap = len1 - len2;
    int max = len1;
    if (len1 < len2)
    {
        longlist = l2;
        shortlist = l1;
        gap = len2 - len1;
        max = len2;
    }

    Add_Node(shortlist, gap);

    Sum_List(longlist, shortlist, max);

    return shortlist;
}